import Vue from 'vue'
import VueRouter from 'vue-router'
import login from "@/components/Login.vue"
import home from "@/components/Home.vue"
import welcome from "@/components/Welcome.vue"
import users from "@/components/user/Users.vue"
import rights from '@/components/power/Rights.vue'
import roles from "@/components/power/Roles.vue"
import cate from "@/components/goods/Cate.vue"
import params from "@/components/goods/Params.vue"
import goodsList from '@/components//goods/List.vue'
import goodsAdd from "@/components/goods/Add.vue"
import order from "@/components/order/Order.vue"
import report from "@/components/report/Report.vue"
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: welcome },
      { path: '/users', component: users },
      { path: '/rights', component: rights },
      { path: '/roles', component: roles },
      { path: '/categories', component: cate },
      { path: '/params', component: params },
      { path: '/goods', component: goodsList },
      { path: '/goods/add', component: goodsAdd },
      { path: '/orders', component: order },
      { path: '/reports', component: report }
    ]
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 判断是否要跳转到登录页
  if (to.path === '/login') return next()

  const token = sessionStorage.getItem('token')
  // 判断token是否存在 不存在跳转到登录页
  if (!token) return next('/login')
  // 其他情况直接放行
  next()
})

export default router
